﻿using EMoney.IStock.Client.Models.MC;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace EMoney.IStock.Client.DataAccess.MC
{
    /// <summary>
    /// 任务信息数据访问类
    /// </summary>
    public class TaskInfoDal
    {
        public List<MC_TaskInfo> GetTaskInfoList()
        {
            string sql = "select * from MC_TaskInfo where IsOpen=1";

            return SQLHelper.GetDataByColumnsLst<MC_TaskInfo>(ConnectionManager.Emfstock, sql, null);
        }

        /// <summary>
        /// 获取用户已完成的单次任务编号及完成时间
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public Dictionary<int, DateTime> GetCompletedTaskIDList(int userid,MC_TaskType taskType,DateTime recordTime)
        {
            SQLTools tools = new SQLTools();
            StringBuilder sql = new StringBuilder();
            Dictionary<int, DateTime> retList = new Dictionary<int, DateTime>();

            sql.Append("SELECT DISTINCT t.TaskID,r.RecordTime FROM dbo.MC_CompleteTaskRecord r");
            sql.Append(" LEFT JOIN dbo.MC_TaskInfo t ON r.TaskID=t.TaskID");
            sql.Append(" WHERE r.UserID=@userid AND t.Type=@Type");

            if (taskType == MC_TaskType.Daily)
            {
                sql.Append(" and r.RecordTime>=@RecordTime");

                tools.AddSqlParameter("RecordTime", System.Data.SqlDbType.DateTime, recordTime);
            }

            tools.AddSqlParameter("userid", System.Data.SqlDbType.Int, userid);
            tools.AddSqlParameter("Type", System.Data.SqlDbType.Int, taskType.GetHashCode());

            DataTable dtResult = SQLHelper.ExecuteGetTable(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql.ToString(), tools.GetSqlParameters());

            if (dtResult != null && dtResult.Rows.Count > 0)
            {
                foreach (DataRow row in dtResult.Rows)
                {
                    try
                    {
                        if(row["TaskID"] != DBNull.Value)
                        {
                            retList.Add(int.Parse(row["TaskID"].ToString()), DateTime.Parse(row["RecordTime"].ToString()));
                        }
                    }
                    catch// (Exception ex)
                    {
                    }                    
                }
            }

            return retList;
        }

        /// <summary>
        /// 获取用户日常任务编号
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="flag">1：已完成  0：未完成</param>
        /// <returns></returns>
//        public List<int> GetDailyTaskIDList(int userid, int flag,DateTime recordDate)
//        {
//            string sql = string.Empty;
//            List<int> retList = new List<int>();

//            switch (flag)
//            {
//                case 1:
//                    sql = @"SELECT DISTINCT t.TaskID FROM dbo.MC_CompleteTaskRecord r
//                            LEFT JOIN dbo.MC_TaskInfo t ON r.TaskID=t.TaskID
//                            WHERE r.UserID=@userid AND t.Type=2 and r.RecordTime>=@RecordTime";
//                    break;
//                case 0:
//                    sql = @"SELECT * FROM dbo.MC_TaskInfo WHERE TaskID NOT IN(
//                            SELECT DISTINCT t.TaskID FROM dbo.MC_CompleteTaskRecord r
//                            RIGHT JOIN dbo.MC_TaskInfo t ON r.TaskID=t.TaskID
//                            WHERE r.UserID=@userid AND t.Type=2 and r.RecordTime>=@RecordTime) AND type=2";
//                    break;
//                default:
//                    break;
//            }

//            SQLTools tools = new SQLTools();

//            tools.AddSqlParameter("userid", System.Data.SqlDbType.Int, userid);
//            tools.AddSqlParameter("RecordTime", System.Data.SqlDbType.DateTime, recordDate);

//            DataTable dtResult = SQLHelper.ExecuteGetTable(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql, tools.GetSqlParameters());

//            if (dtResult != null && dtResult.Rows.Count > 0)
//            {
//                foreach (DataRow row in dtResult.Rows)
//                {
//                    try
//                    {
//                        if (row["TaskID"] != DBNull.Value)
//                        {
//                            retList.Add(int.Parse(row["TaskID"].ToString()));
//                        }
//                    }
//                    catch (Exception ex)
//                    {
//                    }
//                }
//            }

//            return retList;
//        }
    }
}
